package com.example.com.skills_utf8;

import java.lang.reflect.Field;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;


//
// http://bbs.csdn.net/topics/390594156
public class MyTabActivity extends TabActivity{
	
	MyTabActivity activity = this;

	Field mBottomLeftStrip;
	Field mBottomRightStrip;	

	private TabHost tabHost;
	  private TabWidget tabWidget;
	  
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		
		setTheme(android.R.style.Theme_Black);
		
		setContentView(R.layout.tabhost);
		
		
		makeTab();
		
		
		
	}
	
	
	void makeTab(){
		
		
		   TabHost.TabSpec localTabSpec1;
		    TabHost.TabSpec localTabSpec2;
		    TabHost.TabSpec localTabSpec3;
		    TabHost.TabSpec localTabSpec4;
		    
		    
		tabHost =getTabHost();
		tabWidget = getTabWidget();
		tabHost.setup();
		tabHost.bringToFront();
		
		localTabSpec1 =tabHost.newTabSpec("a");
		localTabSpec2 =tabHost.newTabSpec("b");
		localTabSpec3 =tabHost.newTabSpec("c");
		localTabSpec4 =tabHost.newTabSpec("d");
		
		
		Drawable first_drawable =getResources().getDrawable(R.drawable.my_first);
		localTabSpec1.setIndicator("首页", getResources().getDrawable(R.drawable.my_first)).setContent(new Intent(activity, AnimationActivity.class));
	//	localTabSpec2.setIndicator("店铺", getResources().getDrawable(R.drawable.category_select)).setContent(new Intent(activity, LoadMoreListViewActivity.class));
		localTabSpec2.setIndicator("店铺", getResources().getDrawable(R.drawable.second)).setContent(new Intent(activity, EmptyListViewActivity.class));
		localTabSpec3.setIndicator("互动", getResources().getDrawable(R.drawable.third)).setContent(new Intent(activity, MyAChartEngineActivity.class));
		localTabSpec4.setIndicator("店铺评论", getResources().getDrawable(R.drawable.four)).setContent( new Intent(activity, ShopsCommentActivity.class));
		tabHost.addTab(localTabSpec1);
		tabHost.addTab(localTabSpec2);
		tabHost.addTab(localTabSpec3);
		tabHost.addTab(localTabSpec4);
	
		
		
		
		if (Integer.valueOf(android.os.Build.VERSION.SDK).intValue() <= 7)
			try
			{
				mBottomLeftStrip = tabWidget.getClass().getDeclaredField("mBottomLeftStrip");
				mBottomRightStrip = tabWidget.getClass().getDeclaredField("mBottomRightStrip");
				if (!mBottomLeftStrip.isAccessible())
					mBottomLeftStrip.setAccessible(true);
				if (!mBottomRightStrip.isAccessible())
					mBottomRightStrip.setAccessible(true);
				mBottomLeftStrip.set(tabWidget, getResources().getDrawable(R.drawable.linee));
				mBottomRightStrip.set(tabWidget, getResources().getDrawable(R.drawable.linee));
			}
			catch (Exception exception1)
			{
				exception1.printStackTrace();
			}
		else
			try
			{
				mBottomLeftStrip = tabWidget.getClass().getDeclaredField("mLeftStrip");
				mBottomRightStrip = tabWidget.getClass().getDeclaredField("mRightStrip");
				if (!mBottomLeftStrip.isAccessible())
					mBottomLeftStrip.setAccessible(true);
				if (!mBottomRightStrip.isAccessible())
					mBottomRightStrip.setAccessible(true);
				mBottomLeftStrip.set(tabWidget, getResources().getDrawable(R.drawable.linee));
				mBottomRightStrip.set(tabWidget, getResources().getDrawable(R.drawable.linee));
			}
			catch (Exception exception)
			{
				exception.printStackTrace();
			}
		
		
		
		 /* 对Tab标签的定制 */
        for (int i = 0; i < tabWidget.getChildCount(); i++)
        {
            /* 得到每个标签的视图 */
            View view = tabWidget.getChildAt(i);
            /* 设置每个标签的背景 */
            if (tabHost.getCurrentTab() == i)
            {
            	
            	 /* 设置tab内字体的颜色 */
                TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(
                        android.R.id.title);
                tv.setTextColor(Color.rgb(240, 240, 240));
                
            	 //背景图
                 ImageView iconView = (ImageView)tabWidget.getChildAt(i).findViewById(android.R.id.icon);
                 iconView.setImageDrawable(getResources().getDrawable(R.drawable.select)); 
                
            }
            else
            {
            	view.setBackgroundColor(Color.TRANSPARENT);
                /*view.setBackgroundDrawable(getResources().getDrawable(
                        R.drawable.tabbar_bg));*/
                
                /* 设置tab内字体的颜色 */
                TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(
                        android.R.id.title);
                tv.setTextColor(Color.rgb(102, 102, 102));
            }
            /* 设置Tab间分割竖线的颜色 */
            //mTabWidget.setBackgroundColor(Color.RED);
            /* 设置Tab间分割竖线的背景图片 */
            //mTabWidget.setBackgroundColor(Color.BLACK);
            /* 设置tab的高度 */
            
            //mTabWidget.getChildAt(i).getLayoutParams().width = 128;
            /*TextView tv = (TextView) mTabWidget.getChildAt(i).findViewById(
                    android.R.id.title);
                             设置tab内字体的颜色 
            tv.setTextColor(Color.rgb(102, 102, 102));*/
            view.setBackgroundDrawable(getResources().getDrawable(
                    R.drawable.unfocus));
     //OK       tabWidget.getChildAt(i).getLayoutParams().height = 80;
           
        }

		
		//
		tabHost.setOnTabChangedListener( new OnTabChangeListener() {
			
			@Override
			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub
			
				System.out.println("TabChanged: "+tabId);
				
				///*
				int CountTab = tabWidget.getChildCount();
				int CurrentTab =tabHost.getCurrentTab();
				System.out.println("CurrentTab: "+CurrentTab);
			
				for(int i =0; i<CountTab; i++){

					View LocalView =tabWidget.getChildAt( i );
					if(i == CurrentTab){
						LocalView.setBackgroundDrawable(getResources().getDrawable(R.drawable.focus));
					}
					else{
						LocalView.setBackgroundDrawable(getResources().getDrawable(R.drawable.unfocus));
					}
					
				}
				
				//*/
				
			}
		});
		
		
		
	}
	
	
}



